package com.hbnu.system.controller;


import com.hbnu.system.core.base.CommonPage;
import com.hbnu.system.core.base.Rest;
import com.hbnu.system.model.dto.CheckBackDTO;
import com.hbnu.system.model.dto.PaperDTO;
import com.hbnu.system.model.dto.PaperTSdto;
import com.hbnu.system.model.entity.Paper;
import com.hbnu.system.model.entity.Paperfile;
import com.hbnu.system.model.query.PaperPageQuery;
import com.hbnu.system.model.vo.PaperVO;
import com.hbnu.system.service.IPaperService;
import com.hbnu.system.service.IPaperfileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2023-05-16
 */
@RestController
@RequestMapping("/paper")
@Api(tags = "论文模块")
public class PaperController {
    @Autowired
    private IPaperService iPaperService;
    @Autowired
    private IPaperfileService  iPaperfileService;

    @ApiOperation("论文-正式录入")
    @PostMapping("/addPaper")
    public Rest<Boolean> AddPaper(@RequestBody @Valid PaperDTO paperdto){
        boolean add = iPaperService.addPaper(paperdto);
        return add ? Rest.success() : Rest.failed();
    }
    
    @ApiOperation("论文-暂存数据")
    @PostMapping("/saveDraftPaper")
    public Rest<Boolean> saveDraftPaper(@RequestBody PaperTSdto paperTSdto){
        boolean save = iPaperService.saveDraftPaper(paperTSdto);
        return save ? Rest.success() : Rest.failed();
    }


    @ApiOperation("论文-删除")
    @GetMapping("/DeletePaper")
    public Rest<Boolean> DeletePaper(String PaperID) throws IOException {
        boolean delete = iPaperService.deletePaper(PaperID);
        return delete ? Rest.success() : Rest.failed();
    }

    @ApiOperation("论文-修改")
    @PostMapping("/UpdatePaper")
    public Rest<Boolean> UpdatePaper(@RequestBody Paper paper){
        boolean update = iPaperService.updatePaper(paper);
        return update ? Rest.success() : Rest.failed();
    }

    @ApiOperation("论文-提交")
    @GetMapping("/ReCommitPaper")
    public Rest<Boolean> ReCommitPaper(String PaperID){
        boolean add = iPaperService.ReCommitPaper(PaperID);
        return add ? Rest.success() : Rest.failed();
    }
    
    @ApiOperation("论文-撤回")
    @GetMapping("/WithdrawPaper")
    public Rest<Boolean> WithdrawPaper(String id){
        boolean result = iPaperService.withdrawPaper(id);
        return result ? Rest.success() : Rest.failed();
    }


    @ApiOperation("论文-名称查重")
    @GetMapping("/CheckPaperName")
    public Rest<Boolean> CheckPaperName(String paper_name){
        boolean check = iPaperService.checkPaperName(paper_name);
        return check ? Rest.success() : Rest.failed();
    }

    @ApiOperation("根据主键论文查询")
    @GetMapping("/GetPaperById")
    public Rest<Paper> GetPaperById(String paperID){
        Paper paper = iPaperService.getPaper(paperID);
        return Rest.success(paper);
    }

    @ApiOperation("根据主键论文DTO查询")
    @GetMapping("/GetPaperDTOById")
    public Rest<PaperDTO> GetPaperDTOById(String paperID){
        PaperDTO paperdto = iPaperService.getPaperDTOById(paperID);
        return Rest.success(paperdto);
    }

    @ApiOperation("论文-院级通过")
    @GetMapping("/CollegeCheckPass")
    public Rest<String> CollegePassPaper(@RequestHeader("X-Token") String token,String PID){
        Boolean b = iPaperService.collegeCheckPass(token,PID);
        return b ? Rest.success() : Rest.failed();
    }

    @ApiOperation("论文-院级退回")
    @PostMapping("/CollegeCheckBack")
    public Rest<String> CollegeBackPaper(@RequestHeader("X-Token") String token,@RequestBody CheckBackDTO checkBackDTO){
        Boolean b = iPaperService.collegeCheckBack(token,checkBackDTO);
        return b ? Rest.success() : Rest.failed();
    }

    @ApiOperation("论文-校级通过")
    @GetMapping("/SchoolCheckPass")
    public Rest<String> SchoolPassPaper(@RequestHeader("X-Token") String token,String PID){
        Boolean b = iPaperService.schoolCheckPass(token,PID);
        return b ? Rest.success() : Rest.failed();
    }

    @ApiOperation("论文-校级退回")
    @PostMapping("/SchoolCheckBack")
    public Rest<String> SchoolBackPaper(@RequestHeader("X-Token") String token,@RequestBody CheckBackDTO checkBackDTO){
        Boolean b = iPaperService.schoolCheckBack(token,checkBackDTO);
        return b ? Rest.success() : Rest.failed();
    }


    @ApiOperation("用户获取与自己相关的论文")
    @PostMapping("/UserGetPapers")
    public Rest<CommonPage<PaperVO>> UserGetPapers(@RequestBody PaperPageQuery paperPageQuery) {
        CommonPage<PaperVO> paperVOCommonPage =iPaperService.userGetPapers(paperPageQuery);
        return Rest.success(paperVOCommonPage);
    }

    @ApiOperation("论文导出")
    @PostMapping("export")
    public Rest<CommonPage<Paper>> export(@RequestBody PaperPageQuery paperPageQuery){
        CommonPage<Paper> paperCommonPage = iPaperService.export(paperPageQuery);
        return Rest.success(paperCommonPage);
    }


    /**
     * 下载数据库数据为exles
     *
     * @param response
     * @throws IOException
     */
    @ApiOperation("导出excel")
    @RequestMapping(value = "UserExcelDownloads", method = RequestMethod.GET)
    public void downloadAllClassmate(HttpServletResponse response,String id) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("信息表");

        Paper user = iPaperService.getPaper(id);

        String fileName = "userinf"  + ".xls";//设置要导出的文件的名字
        //新增数据行，并且设置单元格数据

        int rowNum = 1;

        String[] headers = { "id", "name", "sex", "create_time"};
        //headers表示excel表中第一行的表头

        HSSFRow row = sheet.createRow(0);
        //在excel表中添加表头

        for(int i=0;i<headers.length;i++){
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }
        HSSFRow row1 = sheet.createRow(rowNum);
        row1.createCell(0).setCellValue(user.getPaperID());
        row1.createCell(1).setCellValue(user.getPaperName());
        row1.createCell(2).setCellValue(user.getStatus());

        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }
    @ApiOperation("论文-文件信息上传")
    @PostMapping("/addpaperfile")
    public Rest<Boolean> addpaperfile(@RequestBody Paperfile paperfile){
        boolean add = iPaperfileService.addPaperfile(paperfile);
        return add ? Rest.success() : Rest.failed();
    }
    @ApiOperation("论文-文件信息删除")
    @GetMapping("/Deletepaperfile")
    public Rest<Boolean> Deletepaperfile(String paperfileID)throws  IOException{
        boolean delete = iPaperfileService.deletPaperfile(paperfileID);
        return delete ? Rest.success() : Rest.failed();
    }
    @ApiOperation("论文-文件信息分页查询")
    @GetMapping ("/pagepaperfileCondition")
    public Rest<List<Paperfile>> pagepaperfileConditionfile(String id){
        List<Paperfile> page = iPaperfileService.pagePaperfile(id);
        return Rest.success(page);
    }
    @ApiOperation("论文-文件信息修改")
    @PostMapping("/updatepaperfile")
    public Rest<Boolean> updatepaperfile(@RequestBody Paperfile paperfile){
        boolean update = iPaperfileService.updatePaperfile(paperfile);
        return update ? Rest.success() : Rest.failed();
    }
}

